package everydayGrade.t2;

import org.junit.jupiter.api.Test;

/**
 * @Author: Siantar
 * @Date: 2023-11-13-19:03
 * @Description: 1.0
 */
public class T0731 {
    class MyCalendarTwo {

        public MyCalendarTwo() {

        }

        public boolean book(int start, int end) {
            System.out.println(query(root,0,N,start,end-1));
            if (query(root,0,N,start,end-1)>=2)return false;
            update(root,0,N,start,end-1,1);
            return true;
        }

        class Node {
            Node left, right;
            int value, change;
        }

        public Node root = new Node();
        public int N = (int) 1e9;

        public void update(Node node, int start, int end, int left, int right, int change) {
            if (left <= start && end <= right) {
                node.value += change;
                node.change += change;
                return;
            }
            int mid = start + ((end - start) >> 1);
            pushDown(node, mid - start + 1, end - mid);
            if (left <= mid) update(node.left, start, mid, left, right, change);
            if (mid < right) update(node.right, mid + 1, end, left, right, change);
            pushUp(node);
        }

        public int query(Node node, int start, int end, int left, int right) {
            if (left <= start && end <= right) {
                return node.value;
            }
            int mid = start + ((end - start) >> 1), ans = 0;
            pushDown(node, mid - start + 1, end - mid);
            if (left <= mid) ans =Math.max(ans,query(node.left, start, mid, left, right)) ;
            if (mid < right) ans =Math.max(ans,query(node.right, mid + 1, end, left, right)) ;
            return ans;
        }


        public void pushUp(Node node) {
            node.value = Math.max(node.left.value, node.right.value);
        }

        public void pushDown(Node node, int leftNum, int rightNum) {
            if (node.left == null) node.left = new Node();
            if (node.right == null) node.right = new Node();
            if (node.change == 0) return;
            node.left.value += node.change;
            node.right.value += node.change;
            node.left.change += node.change;
            node.right.change += node.change;
            node.change = 0;
        }

    }
    @Test
    public void test() {
        MyCalendarTwo m=new MyCalendarTwo();
//        m.update(m.root,0,m.N,24,39,1);
//        System.out.println(m.query(m.root,0,m.N,14,28));
//        m.update(m.root,0,m.N,43,49,1);
//        m.update(m.root,0,m.N,27,42,1);
//        m.update(m.root,0,m.N,5,20,1);
//        m.update(m.root,0,m.N,30,39,1);
        System.out.println(m.query(m.root,0,m.N,14,28));

    }
}
